package com.cdream.basic.algorithm.leetcode.dfs;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cdream
 * @date 5/27/21
 */
public class Question22 {
    public static void main(String[] args) {
        System.out.println(new Question22().generateParenthesis(2));
    }

    public List<String> generateParenthesis(int n) {
        List<String> res = new ArrayList<>();
        dfs(n, "", 0, 0, res);
        return res;
    }

    private void dfs(int n, String path, int start, int end, List<String> res) {
        if (end > start || start > n) {
            return;
        }
        if (2 * n == start + end) {
            res.add(path);
            return;
        }
        dfs(n, path + "(", start + 1, end, res);
        dfs(n, path + ")", start, end + 1, res);

    }
}
